<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;
/**
* 分类控制器
*/
class CategoryController extends CommonController
{
	private $model;
	public function __init()
	{
		parent::__init();
		$this->model=new \Common\Model\Category;
	}
	public function index()
	{
		$data=$this->model->get();
		$data=Data::tree($data,'cname');
		if (!$data) View::success('请先添加分类',U('add'));
		//分配变量
		View::with('data',$data);
		View::make();
	}
	//添加
	public function add()
	{
		$type=Db::table('type')->get();
		if (IS_POST) {
			if (!$this->model->store()) {
				View::error($this->model->getError());
			}
			View::success('添加成功',U('index'));
		}
		View::with('type',$type);
		View::make();
	}
	public function addSon()
	{
		$cid=Q('get.cid',0,'intval');
		if (IS_POST) {
			if (!$this->model->store()) {
				View::error($this->model->getError());
			}
			View::success('添加成功',U('index'));
		}
		$fatherCate=$this->model->where("cid={$cid}")->field('cname,cid,type_tid')->find();
		View::with('fatherCate',$fatherCate);
		$type=Db::table('type')->get();
		View::with('type',$type);
		View::make();
	}
	//删除
	public function del()
	{
		$this->model->del(Q('get.cid',0,'intval'));
		go(U('index'));
	}
	public function edit()
	{
		$cid=Q('get.cid',0,'intval');
		if (IS_POST) {
			if (!$this->model->edit($cid)) View::error($this->model->getError());
			View::success('修改成功',U('index'));
		}
		$type=Db::table('type')->get();
		View::with('type',$type);
		$data=$this->model->where("cid={$cid}")->find();
		View::with('data',$data);
		//获得“所属分类”的数据,调用模型自定义方法
		$cateData=$this->model->getNoMine($cid);
		View::with('cateData',$cateData);
		View::make();
	}
}

 ?>